const http = require('http');
const fs = require('fs');
const path = require('path');

var arr = process.argv;

http.createServer((req, res) => {
    res.writeHead(200, {
        "Content-Type": "text/html;charset=utf-8"
    })
    var filePath = path.join(__dirname, './fileReader1.js');
    if (arr[2]) {
        var filePath1 = path.join(__dirname, arr[2]);
        if (fs.existsSync(filePath1)) {
            fs.readFile(filePath1, (err, data) => {
                if (err) {
                    console.log(err);
                } else {
                    res.end(data.toString('utf8'));
                }
            })
        }
    } else {
        fs.readFile(filePath, (err, data) => {
            if (err) {
                console.log(err);
            } else {
                res.end(data.toString('utf8'));
            }
        })
    }


}).listen(8081);
console.log('server listening on port');